package com.own.component.elastic.model;

import co.elastic.clients.elasticsearch._types.FieldValue;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * CustomFieldValue
 *
 * @author chenxueli
 * @date 2024-03-01 16:40:00
 */
@Data
@NoArgsConstructor
public class CustomFieldValue implements Serializable {

    /**
     * 类型
     */
    private String type;

    /**
     * 值
     */
    private String value;

    public CustomFieldValue(FieldValue fieldValue) {
        this.type = fieldValue._kind().toString();
        this.value = fieldValue._get().toString();
    }

    public FieldValue convert() {
        return switch (type) {
            case "Boolean" -> FieldValue.of(Boolean.parseBoolean(value));
            case "Long" -> FieldValue.of(Long.parseLong(value));
            case "Double" -> FieldValue.of(Double.parseDouble(value));
            default -> FieldValue.of(value);
        };
    }

}
